home *** CD-ROM | disk | FTP | other *** search
- Path: gryphon.phoenix.net!usenet
- From: brucew@phoenix.net (Bruce Wedding)
- Newsgroups: comp.lang.c
- Subject: Re: HELP!! BAMBOOZLED BEGINNER!!
- Date: Fri, 02 Feb 1996 03:28:31 GMT
- Organization: BranPaul Systems
- Message-ID: <4eru59$s8t@gryphon.phoenix.net>
- References: <Pine.OSF.3.91l.960130235948.20497A-100000@saul3.u.washington.edu>
- NNTP-Posting-Host: dial8.phoenix.net
- X-Newsreader: Moe's Newsreader
-
- >However, when I try to raise 5 to the 7th power, I get "12589"
- >when it really should be "78125". My teacher told me that instead of using
- >plain 'int', I should use 'long int'.
-
- If your ints are 16 bits, your teacher is correct. The upper
- limit of a 16 bit signed int is about 32,766.
-
- > int i, /* integer */
- Since you aren't accepting negative values for the exponent, make
- this:
- unsigned long int i, i_total;
- That will give the largest range without resorting to floating
- point.
-
- > scanf("%d", &i);
-
- change this to scanf("%U", &i);
-
-
- > i_total = 1; /* gives i_total an initial total of '1' */
-
- > for (count = 1; count <= n; count = count + 1){
- > i_total = i_total * i;
- > }
-
- for (count = 0; count < n; count++)
- {
- i_total *= i;
-
- printf("%ul raised to the %dth power is %ul\n\n", i, n, i_total);
-
- Here is a tested version. There is still no overflow protection.
-
- #include <stdio.h>
-
- int main()
- {
- unsigned long int i = 0, i_total = 0;
- int power = 0, n = 0, scanned = 0;
-
- while (!scanned)
- {
- puts("Enter number");
- scanned = scanf("%U*%c", &i);
- }
- scanned = 0;
- while (!scanned)
- {
- puts("Enter exponent");
- scanned = scanf("%U*%c", &power);
- }
- i_total = 1; /* gives i_total an initial total of '1' */
- for (n = 0; n < power; n++)
- i_total *= i;
- printf("%lu raised to the %dth power is %lu\n\n", i, n,
- i_total);
- return 0;
- }
-
-
-
-
-
-
- Bruce D. Wedding Have Compiler, Will Travel!
- Perspicacious Programming Performed Promptly
- Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
-
-